package com.atguigu.spzx.product.service.impl;

import com.alibaba.fastjson.JSON;
import com.atguigu.spzx.model.dto.h5.ProductSkuDto;
import com.atguigu.spzx.model.entity.product.Product;
import com.atguigu.spzx.model.entity.product.ProductDetails;
import com.atguigu.spzx.model.entity.product.ProductSku;
import com.atguigu.spzx.model.vo.h5.ProductItemVo;
import com.atguigu.spzx.product.mapper.ProductDetailsMapper;
import com.atguigu.spzx.product.mapper.ProductMapper;
import com.atguigu.spzx.product.mapper.ProductSkuMapper;
import com.atguigu.spzx.product.service.ProductService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * ClassName: ProductServiceImpl
 * Package:
 * Description:
 *
 * @Author:xiaohei
 * @Create 2024/5/22 9:24
 * Version 1.0
 */
@Service
public class ProductServiceImpl implements ProductService {
    @Resource
    private ProductSkuMapper productSkuMapper;
    @Resource
    private ProductMapper productMapper;
    @Resource
    private ProductDetailsMapper productDetailsMapper;

    //2  根据销量排序，获取前十条记录
    @Override
    public List<ProductSku> selectProductSkuBySal() {
        List<ProductSku> list = productSkuMapper.selectProductSkuBySal();
        return list;
    }

    //分页查询
    @Override
    public PageInfo<ProductSku> findByPage(Integer page, Integer limit, ProductSkuDto productSkuDto) {
        PageHelper.startPage(page, limit);
        List<ProductSku> productSkuList = productSkuMapper.findByPage(productSkuDto);
        return new PageInfo<>(productSkuList);
    }

    //商品详情接口
    @Override
    public ProductItemVo item(Long skuId) {
        //1 创建vo对象  用于封装最终数据
        ProductItemVo productItemVo = new ProductItemVo();

        //2 根据skuId获取sku信息
        ProductSku productSku = productSkuMapper.getById(skuId);

        //3 根据第二步获取sku  从sku中获取productId  获取商品信息
        Long productId = productSku.getProductId();
        Product product = productMapper.getById(productId);

        //4  根据商品id  获取商品详情
        ProductDetails productDetails = productDetailsMapper.getByProductId(productId);
        //5 封装一个map集合 == 商品规格对应商品sku信息
        Map<String,Object> skuSpecValueMap = new HashMap<String,Object>();
        //根据商品id获取商品所有sku列表
        List<ProductSku> productSkuList = productSkuMapper.findByProductId(productId);
        productSkuList.forEach(item -> {
            skuSpecValueMap.put(item.getSkuSpec(),item.getId());
        });

        //6 需要的数据封装到productItemVo里面
        productItemVo.setProduct(product);
        productItemVo.setProductSku(productSku);
        productItemVo.setSkuSpecValueMap(skuSpecValueMap);

        //封装详情图片list集合
//        String imageUrls = productDetails.getImageUrls();
//        String[] split = imageUrls.split(",");
//        List<String> list = Arrays.asList(split);
        productItemVo.setDetailsImageUrlList(Arrays.asList(productDetails.getImageUrls().split(",")));
        //封装轮播图的信息
        productItemVo.setSliderUrlList(Arrays.asList(product.getSliderUrls().split(",")));
        //封装商品规格信息
        productItemVo.setSpecValueList(JSON.parseArray(product.getSpecValue()));
        return productItemVo;
    }

    //根据skuId获取sku信息
    @Override
    public ProductSku getBySkuId(Long skuId) {
        ProductSku productSku  = productSkuMapper.getById(skuId);
        return productSku;
    }
}
